﻿/*****************************************************************************
* File			: src.PowerUp
* Purpose		: Contains Declaration and Implementation for PowerUp class
* Creation date : April 24, 2009
* Author		: Sami TOUIL
******************************************************************************/
package src 
{
	// Imports
	import flash.display.MovieClip;
	import flash.events.*
	import flash.ui.*
	import flash.utils.Timer
	
	/**************************************
	* Class		: PowerUp
	* Purpose	: Logic for PowerUp behavior
	* Parent	: MovieClip
	**************************************/
	public class PowerUp extends MovieClip
	{
		// Class variables
		public 	var nSpeed		:int;		// PowerUp speed
		public 	var nRotation	:int;		// Rotation of the powerup
		public 	var nType		:int;		// Power up type : Health, Destroy all or Speed
		
		// Constructor
		public function PowerUp()
		{			
			// Set listener for ENTER_FRAME event
			addEventListener(Event.ENTER_FRAME, enterFrame);
			
			// Set a fixed speed but a random type and a random rotation
			nSpeed = 2;
			nType = Math.floor(Math.random()*4+1);					// 1, 2, 3 or 4
			
			// If background is stopping, we can't have speed bonus (type 3), transform it into Health bonus
			if (nType == 3 && Background.nSpeed < 2)
				nType = 1;
			
			nRotation = 2 * (1 - (Math.floor(Math.random()*2) *2)); // 2 or -2
			x = 650;
			y = Math.random()*200+50;
			
			// Add Power up sprite
			Game.main.spriteClip.addChild(this);
		}
		
		/***********************************************************
		* Method		: onDelete
		* Purpose		: called for deleting the object instance
		* Parameters	: None
		* return		: void
		***********************************************************/
		public function onDelete()
		{
			removeEventListener(Event.ENTER_FRAME, enterFrame);
			Game.main.spriteClip.removeChild(this);
		}
		
		/***********************************************************
		* Method		: enterFrame
		* Purpose		: frame loop
		* Parameters	: e:Event
		* return		: void
		***********************************************************/
		public function enterFrame(e:Event)
		{
			// Clear if the hero ship is dead
			if (Game.main.ship == null)
			{
				onDelete();
				return;
			}
			else
			{
				// Move and rotate
				x -= nSpeed;
				rotation -= nRotation;
			
				// Collision management
				if(this.hitTestObject(Game.main.ship))
				{		
					// Healing power
					if(nType == 1)
					{
						// Add 20 health
						Game.main.ship.updateHealth(20);
						
						// dispay rewardpoint
						var healReaward = new RewardPoint(x, y, "20", 0x66CC66);
						Game.main.spriteClip.addChild(healReaward);
					}
					// Kill'em all
					if(nType == 2)
					{
						// kill all Enemy instances
						Enemy.onTakeDamageAll();
						
						// dispay rewardpoint
						var killReward = new RewardPoint(x, y, "Kill'em all", 0x333333);
						Game.main.spriteClip.addChild(killReward);
					}
					// Speed
					if(nType == 3)
					{	
						// Use timers to temporarily and progressively increase background speed (5seconds)
						var accelerationTimer:Timer = new Timer(1000/60,60);
						accelerationTimer.addEventListener(TimerEvent.TIMER, Background.accelerationHandler);
						accelerationTimer.start();
						
						var speedTimer = new Timer(5000,1);
						speedTimer.addEventListener(TimerEvent.TIMER_COMPLETE, speedHandler); // This will be used to reset Speed
						speedTimer.start();
						
						// Increase ship speed
						Game.main.ship.nSpeed += 3;
						
						// dispay rewardpoint
						var speedReward = new RewardPoint(x, y, "Speed", 0x333333);
						Game.main.spriteClip.addChild(speedReward);
					}
					// Module
					if (nType == 4)
					{
						if (Game.main.ship.module1 == null)
							Game.main.ship.module1 = new Module(Game.main.ship.x, Game.main.ship.y, 1);
							
						else if(Game.main.ship.module2 == null)
							Game.main.ship.module2 = new Module(Game.main.ship.x, Game.main.ship.y, -1);
						
						// dispay rewardpoint
						var moduleReward = new RewardPoint(x, y, "Module", 0x333333);
						Game.main.spriteClip.addChild(moduleReward);
					}
					
					// remove PowerUp
					onDelete();
				}
					
					// If it's not on the stage anymore
				if( x < 0 )
				{
					// remove PowerUp
					onDelete();
				}
			}
		}
		
		/***********************************************************
		* Method		: speedHandler
		* Purpose		: Triggered when ending speed powerup
		* Parameters	: e:Event
		* return		: void
		***********************************************************/
		public function speedHandler(event:TimerEvent):void 
		{
			// Trigger Background deceleration and set ship speed variable back to normal
			var slowingTimer:Timer = new Timer(2000/60,60);
			slowingTimer.addEventListener(TimerEvent.TIMER, Background.slowingHandler);
			slowingTimer.start();
			
			if (Game.main.ship != null)
				Game.main.ship.nSpeed -= 3;     
        }

	}
}